<template>
	<scroll-view 
		:scroll-left="scrollLeft"
		:show-scrollbar="false"
		:scroll-x="true"
		:scroll-with-animation="true"
		class="scrollView"
	>
	<view class="tabs">
		<view
			class="item" 
			v-for="(item, index) in list"
			:key="index"
			:style="{width: list.length > 5 ? '140rpx' : 750 / list.length + 'rpx'}"
			@click="tabChange(index)"
		>
		 <text :class="['title', current === index ? 'active' : '']">{{item.title}}</text>
		</view>
	</view>
	</scroll-view>
</template>

<script>
	export default{
		name: 'tabs',
		props:{
			current: {
				type: Number,
				default: 0,
			},
			list:{
				type: Array,
				required: true
			}
		},
		data(){
			return{
				scrollLeft: 0,
				scale: 1,
			}
		},
		created(){
			const systemInfo = uni.getSystemInfoSync()
			this.scale = systemInfo.screenWidth / 750
		},
		methods:{
			tabChange(index){
				if(this.current === index) return
				this.$emit('onChnage', index)
				if(index > 2){
					this.scrollLeft = index * 140 * this.scale - 280 * this.scale
				}else{
					this.scrollLeft = 0
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scrollView{
		display: flex;
		flex-direction: row;
		align-items: center;
		.tabs{
			display: flex;
			flex-direction: row;
			align-items: center;
			height: 44px;
			.item{
				.title{
					font-size: 32rpx;
					text-align: center;
					&.active{
						color: #0EAD96;
						font-weight: bold;
					}
				}
			}
		}
	}
</style>
